<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
%>
<jsp:include page="../../import.jsp" flush="true" />
<style>
.roleView{margin:1px;border:1px solid #15a4d0;float:left;height:25px;padding-left:2px;padding-right:2px;padding-top:5px;cursor:pointer;}
</style>
<body>
<div class="searchSpan"></div>
<div  class="resultSpan"></div>
<div id="editDiv" title="角色关系编辑操作" style="display:none">		
		<TABLE>
		<TR>
			<TD valign='top' >
			<FORM METHOD='POST' >
				<INPUT TYPE="hidden" NAME="index"   id="index" value=''>
				<INPUT TYPE="hidden" NAME="rid"   id="rid" value=''>
				<INPUT TYPE="hidden" NAME="oid"   id="oid" value=''>
				<INPUT TYPE="hidden" NAME="relation" id="relation"  value='' >
			</FORM>
			<TABLE >
			<TR>
					<TD nowrap>用户角色：</TD>
					<TD><!-- onchange="$('#rid').val(this.options[this.selectedIndex].value)" -->
					<SELECT NAME="name" id="name" 
					onchange="var val=this.options[this.selectedIndex].value; if(val!=null)$('#rid').val(val)" >
							<option>请选择用户角色</option>
							 <SCRIPT LANGUAGE="JavaScript">
								queryDB({index:'quaryRoles',pid:'>0'},function(result){
									for(var i in result){
										var rr = result[i];
										document.write('<option value="'+rr['ID']+'">'+rr['NAME']+'</option>');
									}
								},false);
							 </SCRIPT>
					</SELECT>
					</TD>
			</TR>
			<TR>
					<TD colspan='2' >
					<fieldset><legend>已选定角色预览</legend>
						<div id="relationList"></div>
					</fieldset>
					<div style="color:red;font-size:11px;width:100%;text-align:center">*提示:双击预览内容删除已选*</div>
					</TD>
			</TR>
			</TABLE>
			
			</TD>
			<TD valign='top'>
				<jsp:include page="QD.roleTree.jsp" flush="true" >
					<jsp:param name="type" value="checkbox"/>
				</jsp:include>
			</TD>
		</TR>
		</TABLE>			
</div>

</body>
<SCRIPT LANGUAGE="JavaScript">
var tableParam = {
	div:'.resultSpan',
	css:'text-align:center;width:95%',
	queryParam:'index=queryDataRoleList',
	title:null,pagination:'PAGE_NUMBER',
	isDownResult:false,
	elemt:{
		'角色名':{key:'NAME',order:true},
		'拥有角色':{key:'RELATION',order:true,method:function(data){return formatRoleName(data)}},
		'操作[<A HREF="javascript:addNewUrlRule()">新增赋权</A>]':
			{describe:function(rb){
				return '[<A HREF="#" id="tableDataEdit">修改</A>]/[<A HREF=\'javascript:delRoleRelation("'+rb["RID"]+'","'+rb["NAME"]+'")\'>删除</A>]';
		}}
	},bind:{
		'#tableDataEdit':{
			'click':function(rb){
				rb['INDEX'] = 'roleRealationMaintainEdit';
				rb['CTYPE'] = rb.RELATION;
				rb['OID'] = rb.RID;
				infoView.showWithTable(rb);
				
				ownRoleList = {};
				var tmp = rb.RELATION.split(';');
				for(i in tmp){
					ownRoleList[tmp[i]] = formatRoleName(tmp[i]);
				}
				createOverView();
			}	
		}
	}
};

QueryTable(tableParam);

setSearch($('.searchSpan'),tableParam,{
	'NAME':{title:'角色名'},
	'RELATION':{title:'拥有角色',type:'select',data:{index:'quaryRoles',pid:'>0'},text:'NAME',value:'ID',
		method:function(key,value,queryTableParam){
			queryTableParam[key] = '%'+value+'%';
		}}
});

var infoView = new MoreInfoView('角色赋权编辑',500,300,function(){
	_submit();
});
infoView.setContent($('#editDiv'));

var addNewUrlRule = function(){
	ownRoleList = {};
	infoView.showWithData({'index':'roleRealationMaintainInsert'});
}

var _submit = function(fData){
	if(fData==null){
		fData = infoView.toJson();
		fData['relation'] = fData['cType'];
		delete fData['cType'];
		delete fData['name'];
	}
	if(fData['rid'] == null||fData['rid']==''|| fData['relation']==null||fData['relation']==''){
		alert('对不起，用户角色和关联角色必须设置！');
		return;
	}
	//fData['relation'] = fData['relation'].replace(/;/g,'|');
	ajaxUpdate({
		isMulti:false,
		data:fData,
		success:function(rs){
			infoView.close();
			if(rs.json>0){
				QueryTable(tableParam);
			}
		}
	});	
}
var delRoleRelation = function(id,name){
	if(confirm('确定删除角色['+name+']的所拥有角色？')){
		_submit({'index':'roleRealationMaintainDel','rid':id});
	}
}
var ownRoleList = {};
$('.dtree').find(':checkbox').live('click',function(){
	if(this.checked)
		ownRoleList[this.value] = formatRoleName(this.value);
	else
		delete ownRoleList[this.value];
	createOverView();
});

$('.roleView').live('dblclick',function(){
	var obj = $(this);
	$('.dtree').find('input[value="'+obj.attr('val')+'"]').attr('checked',false);
	delete ownRoleList[obj.attr('val')];
	obj.remove();

});
var createOverView = function(){
	var tmp = '';
	for(each in ownRoleList){
		tmp += '<div class="roleView" val="'+each+'">'+ownRoleList[each]+'</div>';
	}
	$('#relationList').html(tmp);
}
</SCRIPT>